-- UIDailyTaskInfo
-- Created by chendh Aug/12/2015
-- 日常任务信息界面

require "game/ui/form/main/UISubAreaList"

UIDailyTaskInfo = class("UIDailyTaskInfo", function()
    return cc.Layer:create();
end);

function UIDailyTaskInfo.create(index)
    return UIDailyTaskInfo.new(index);
end

-- 构造函数
function UIDailyTaskInfo:ctor(index)
    self:setName("UIDailyTaskInfo");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/dailytask/DailyTaskInfo.csb");
    self:addChild(node);
    self.node = node;
    self.index = index;

    local taskList = DailyTaskM.getTaskList();
    self.taskInfo = taskList[index];
    self.taskId = self.taskInfo.id;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 适配
    -- 注：这里适配必须在redraw之前，因为redraw中会重新设置scrollview的尺寸
    self:resize();

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();
end

-- 适配
function UIDailyTaskInfo:resize()
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local node = self.node;

    -- bg/mask居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT中间居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UIDailyTaskInfo:registerTouchEvent()
    local BT = findChildByName(self.node, "BT");

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(BT, "btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            UIMgr.getCurrentScene():removeFormByName("UIDailyTaskInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册前往迷宫按钮点事件
    local btnGo = findChildByName(BT, "btn_go");
    local function onGoClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 前往指定迷宫
            AudioM.playFx("button_click");
            local curDungeonId = ME.user.dbase:query("current_dungeon") or DungeonM.getDungeonId();
            local dungoneId    = self.taskInfo.dungeon_id;

            -- 如果要进入的迷宫和已经有的迷宫不一致，则直接拦住
            if curDungeonId ~= 0 and curDungeonId ~= self.taskInfo.dungeon_id then
                local currName = DungeonAreaM.query(DungeonAreaM.getParentId(curDungeonId), "name");
                local newName  = DungeonAreaM.query(dungoneId, "name");
                alert(string.format(getLocStr("dungeon_pause_tips"), currName, newName));
                return;
            end

            if UIMgr:getCurrentScene():isOpen("UISubAreaList") then
                UIMgr:getCurrentScene():removeForm("UISubAreaList");
            end

            local uiForm = UISubAreaList.create(self.taskInfo.dungeon_id);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end
    btnGo:addTouchEventListener(onGoClick);

    local bonusItemNode = findChildByName(self.node, "CT/bonus_item");
    local bg = findChildByName(self.node, "CT/bonus_item/bg");
    bg:addTouchEventListener(function(sender, eventType)
        local oldScale = 0.8;
        if eventType == ccui.TouchEventType.began then
            -- 1.变小
            local downTime  = 0.25/2;
            local scaleDown = cc.ScaleTo:create(downTime, 0.8 * oldScale);
            bonusItemNode:runAction(scaleDown);
        elseif eventType == ccui.TouchEventType.ended then
            -- 2.变大
            local downTime  = 0.25/2;
            local scaleUp = cc.ScaleTo:create(downTime, 1 * oldScale);
            bonusItemNode:runAction(scaleUp);
        elseif eventType == ccui.TouchEventType.canceled then
            -- 3.变大
            local downTime  = 0.25/2;
            local scaleUp = cc.ScaleTo:create(downTime, 1 * oldScale);
            bonusItemNode:runAction(scaleUp);
        end

        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("workshop_material");

            if ArtifactM.isArtifact(bonusItemNode.classId) then
                -- 如果是神器，显示神器信息
                showEquipTooltip(bonusItemNode.classId);
            else
                -- 显示道具详细信息
                local uiItemDesc = UIItemDesc.create(bonusItemNode.classId);
                UIMgr:getCurrentScene():addForm(uiItemDesc);
            end
        end
    end);
end

-- 注册事件处理回调函数
function UIDailyTaskInfo:registerEventCallback()
    -- 注册日常任务信息变化的事件回调
    EventMgr.register("UIDailyTaskInfo", event.USER_INFO_UPDATED, function(args)
        if args == "daily_task_info" then
            local taskList = DailyTaskM.getTaskList();
            self.taskInfo = taskList[self.index];
            self.taskId = self.taskInfo.id;
            self:redraw();
        end
    end);

    -- 注册获得焦点的回调
    EventMgr.register("UIDailyTaskInfo", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UIDailyTaskInfo" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_GEM);
        end
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDailyTaskInfo 界面析构清理");
            EventMgr.removeAll("UIDailyTaskInfo");
        elseif eventType == "enter" then
            -- 播放界面音效
            AudioM.playFx("market_open");
        end
    end);
end

-- 重绘
function UIDailyTaskInfo:redraw()
    local node = self.node;

    -- 设置标题文本
    local titleLabel = findChildByName(node,"CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(DailyTaskM.query(self.taskId, "name"));

    -- 设置副标题文本
    local subheadLabel = findChildByName(node,"CT/subhead");
    TextStyleM.setSubheadStyle(subheadLabel);
    subheadLabel:setString(getLocStr("daily_task"));

    -- 任务描述
    local taskDesc = "";
    if DailyTaskM.query(self.taskId, "type") == "inherit" then
        taskDesc = string.format(getLocStr(DailyTaskM.query(self.taskId, "desc")), DailyTaskM.query(self.taskId, "desc_arg"), CampM.query(self.taskInfo.camp_id, "name"));
    else
        taskDesc = string.format(getLocStr(DailyTaskM.query(self.taskId, "desc")), DailyTaskM.query(self.taskId, "desc_arg"));
    end

    -- 设置文本及格式
    local descLabel = findChildByName(node,"CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
    descLabel:setString(taskDesc);

    -- 任务目标
    local aimDesc = ""
    if DailyTaskM.query(self.taskId, "type") == "inherit" then
        aimDesc = string.format(getLocStr("daily_task_aim3"), DungeonAreaM.query(self.taskInfo.dungeon_id, "name"), self.taskInfo.floor, CampM.query(self.taskInfo.camp_id, "name"));
    elseif DailyTaskM.query(self.taskId, "type") == "snares_animal" then
        aimDesc = string.format(getLocStr("daily_task_aim2"), DungeonAreaM.query(self.taskInfo.dungeon_id, "name"), self.taskInfo.floor, MonsterM.query(DailyTaskM.query(self.taskInfo.id, "task_monster"), "name"));
    else
        aimDesc = string.format(getLocStr("daily_task_aim1"), DungeonAreaM.query(self.taskInfo.dungeon_id, "name"), self.taskInfo.floor, ItemM.query(DailyTaskM.query(self.taskInfo.id, "task_item"), "name"));
    end

    -- 加上 "奖励："
    aimDesc = aimDesc .. "\n\n";

    -- 设置文本及格式
    local aimLabel = findChildByName(node,"CT/aim");
    TextStyleM.setTextStyle(aimLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE, false, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
    aimLabel:setString(aimDesc);
    local posY = descLabel:getPositionY() - descLabel:getVirtualRenderer():getContentSize().height - 20;
    aimLabel:setPositionY(posY);

    -- 任务奖励
    local bonusItemNode = findChildByName(node, "CT/bonus_item");
    local itemIcon = findChildByName(bonusItemNode, "icon");
    local itemNum = findChildByName(bonusItemNode, "num");
    local starNode = findChildByName(bonusItemNode, "star_node");
    local taskBonus = DailyTaskM.queryBonus(self.taskInfo.bonus_id, "bonus");
    local starNum = 0;
    if taskBonus[1] == 1 then
        itemIcon:loadTexture(getItemIconPath(ItemM.query(taskBonus[2], "icon")));
        starNum = ItemM.query(taskBonus[2], "rank");
    elseif taskBonus[1] == 2 then
        itemIcon:loadTexture(getSmallAttribIconPath(taskBonus[2]));
    elseif taskBonus[1] == 3 then
        itemIcon:loadTexture(getHeroIconPath(taskBonus[2]));
    end

    bonusItemNode.classId = taskBonus[2];
    bonusItemNode:setScale(0.8);
    local posY = aimLabel:getPositionY() - aimLabel:getVirtualRenderer():getContentSize().height - 45;
    bonusItemNode:setPositionY(posY);

    -- 数量
    TextStyleM.setTextStyle(itemNum, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, -2);
    itemNum:setString(tostring(taskBonus[3]));

    -- 星级
    if starNum > 0 then
        -- 显示星星
        starNode:setVisible(true);
        local tStar = createStarLayer(starNum, "images/ui/symbol/star_little.png", "center", -0.7);
        if tStar ~= nil then
            starNode:addChild(tStar);
        end
    else
        starNode:setVisible(false);
    end


    local dungeonId = self.taskInfo.dungeon_id;

    -- 如果有可以通过扫荡完成的话提示
    if AchievementM.hasFinishTaskProp(dungeonId) then

        local dungeonName = DungeonAreaM.query(dungeonId, "name");
        local mopTaskDesc = getLocStr("mop_up_task_desc");
        mopTaskDesc = string.format(mopTaskDesc, dungeonName);

        local mopupTip = findChildByName(self.node, "CT/mop_up_tip");
        mopupTip:setVisible(true);
        TextStyleM.setTextStyle(mopupTip, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE, false);
        mopupTip:setString(mopTaskDesc);

        local bonusItemPosY = bonusItemNode:getPositionY();
        local itemBg = findChildByName(bonusItemNode, "bg");
        local itemBgHeight = itemBg:getContentSize().height;

        mopupTip:setPositionY(bonusItemPosY - itemBgHeight/2);
    end

    -- 设置返回按钮的文本样式
    local btnBack = findChildByName(node, "BT/btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 设置前往迷宫按钮的文本样式
    local btnGo = findChildByName(node, "BT/btn_go");
    TextStyleM.setTextStyle(btnGo, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnGo:setTitleText(getLocStr("btn_text_go_dungeon"));
end
